package hgx.spring.cloud.lession.productrepertory.dao;/**
 * Created by liujun on 2018/3/27.
 */

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;

import hgx.spring.cloud.lession.productrepertory.api.ProductRepository;
import hgx.spring.cloud.lession.productrepertory.vo.ProductVo;

/**
 * @author Administrator
 * @create 2018-03-27 16:42
 */
@Repository
public class ProductDaoImpl implements ProductRepository {
    private static Random random=new Random();
    private List<ProductVo> productVos = new CopyOnWriteArrayList<>();

    @Value("${product.getProductById.sleepTime}")
    private int getProductById;

    @Value("${product.findAll.sleepTime}")
    private int findAll;

    @Value("${product.findProductByIds.sleepTime}")
    private int findProductByIds;

    public ProductDaoImpl() {
        initData();
    }

    @Override
    public boolean createPorduct(ProductVo productVo) {
        if(productVo == null){
            return false;
        }

        if(productVos == null){
            productVos=new ArrayList<>(1);
        }


        productVo.setId((long) (productVos.size() + 1));
        productVos.add(productVo);
        return true;
    }

    @Override
    public ProductVo getProductById(Long id) {
        int threadTime=random.nextInt(getProductById);
        System.out.println("****** Dao getProductById  thread Time: "+threadTime);
        try {
            TimeUnit.MILLISECONDS.sleep(threadTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if(id == null || productVos == null){
            return null;
        }

        for(ProductVo vo : productVos){
            if(vo !=null &&  id.equals(vo.getId())){
                return vo;
            }
        }
        return null;
    }

    @Override
    public List<ProductVo> findAll() {
        int threadTime=random.nextInt(findAll);
        System.out.println("****** Dao findAll  thread Time: "+threadTime);
        try {
            TimeUnit.MILLISECONDS.sleep(threadTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        return productVos;
    }

    @Override
    public List<ProductVo> findProductByIds(List<Long> ids) {
        int threadTime=random.nextInt(findProductByIds);
        System.out.println("****** Dao findProductByIds  thread Time: "+threadTime);
        try {
            TimeUnit.MILLISECONDS.sleep(threadTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if(ids == null || ids.isEmpty() || productVos == null || productVos.isEmpty()){
            return null;
        }

        List<ProductVo> productVoList=new ArrayList<>(ids.size());
        for(Long id : ids){
            for(ProductVo vo : productVos){
                if(id !=null && vo !=null && id.equals(vo.getId())){
                    productVoList.add(vo);
                    continue;
                }
            }
        }
        return productVoList;
    }

    private void initData() {
        ProductVo vo1 = new ProductVo();
        vo1.setId(1L);
        vo1.setName("茅台酒");
        vo1.setNum(120);
        vo1.setPrice(2280.0f);

        productVos.add(vo1);


        ProductVo vo2 = new ProductVo();
        vo2.setId(2L);
        vo2.setName("大米");
        vo2.setNum(1680);
        vo2.setPrice(3.2f);

        productVos.add(vo2);

        ProductVo vo3 = new ProductVo();
        vo3.setId(3L);
        vo3.setName("卫生纸");
        vo3.setNum(860);
        vo3.setPrice(4.5f);

        productVos.add(vo3);

        ProductVo vo4 = new ProductVo();
        vo4.setId(4L);
        vo4.setName("国酒香香烟");
        vo4.setNum(67);
        vo4.setPrice(1600.0f);

        productVos.add(vo4);

    }
}
